/**
 * Copyright 2021 xpstem.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.xpstem.lelink.device;

import com.xpstem.lelink.EventBus;
import com.xpstem.lelink.command.EV3DirectCommand;
import com.xpstem.lelink.enums.InputPort;
import com.xpstem.lelink.operation.EV3Operation;
import com.xpstem.lelink.operation.EV3InputOpFactory;
import com.xpstem.lelink.operation.ReturnParam;
import com.xpstem.lelink.type.Data32;
import com.xpstem.lelink.type.Data8;
import com.xpstem.lelink.type.DataF;

import java.io.IOException;
import java.util.Map;

/**
 * EV3 sensor base class.
 *
 * @author billy zhang
 */
public abstract class EV3Sensor implements EV3InputDevice {

    private EventBus eventBus;
    private InputPort portNo;
    private String name;
    private short type;
    private short mode;
    private Map<Short, String> modeMap;

    private int rawValue;
    private int pctValue;
    private float siValue;

    public EV3Sensor(EventBus eventBus, InputPort portNo, String name,
                     short type, short mode) {
        this.eventBus = eventBus;
        this.portNo = portNo;
        this.name = name;
        this.type = type;
        this.mode = mode;
    }

    @Override
    public InputPort getInPort() { return portNo; }

    @Override
    public short getType() { return type; }

    @Override
    public String getName() { return name; }

    @Override
    public short getMode() { return mode; }

    public void readData() throws IOException {

        ReturnParam<Data32> rawParam = new ReturnParam<>(0, Data32.from(0));
        ReturnParam<DataF> siParam = new ReturnParam<>(4, DataF.from(0));
        ReturnParam<Data8> pctParam = new ReturnParam<>(8, Data8.from(0));
        EV3Operation readyRawOp = EV3InputOpFactory.OpInputDevice.readyRaw(
                eventBus.getLayer(), portNo, Data8.from(0), Data8.from(mode), Data8.from(1), rawParam);
        EV3Operation readySiOp = EV3InputOpFactory.OpInputDevice.readySi(
                eventBus.getLayer(), portNo, Data8.from(0), Data8.from(mode), Data8.from(1), siParam);
        EV3Operation readyPctOp = EV3InputOpFactory.OpInputDevice.readyPct(
                eventBus.getLayer(), portNo, Data8.from(0), Data8.from(mode), Data8.from(1), pctParam);

        EV3DirectCommand cmd = new EV3DirectCommand(readyRawOp, readySiOp, readyPctOp);
        eventBus.postCommand(cmd);

        int value = rawParam.getValue().getValue();
        if (value == rawValue) {
            return;
        }

        rawValue = value;
        pctValue = pctParam.getValue().getValue();
        siValue = siParam.getValue().getValue();

        eventBus.onSensorChanged(portNo, EV3Sensor.this);

    }

    public void updateValue(int rawValue, int pctValue, float siValue) {
        if (rawValue == this.rawValue) {
            return;
        }

        this.rawValue = rawValue;
        this.pctValue = pctValue;
        this.siValue = siValue;

    }

    public int getRawValue() { return rawValue; }

    public int getPctValue() { return pctValue; }

    public float getSiValue() { return siValue; }

    @Override
    public boolean ready() throws IOException {
        EV3Operation op = EV3InputOpFactory.opInputReady(
                eventBus.getLayer(), portNo );
        EV3DirectCommand cmd = new EV3DirectCommand(op);
        eventBus.postCommand(cmd);
        return true;
    }

    /**
     * 返回传感器的原始度量值
     * @param mode
     * @return
     */
    public int readyRaw(short mode) throws IOException {
        ReturnParam<Data32> rawParam = new ReturnParam<>(0, Data32.from(0));
        EV3Operation op = EV3InputOpFactory.OpInputDevice.readyRaw(
                eventBus.getLayer(), portNo, Data8.from(0), Data8.from(mode), Data8.from(1), rawParam );
        EV3DirectCommand cmd = new EV3DirectCommand(op);
        eventBus.postCommand(cmd);
        rawValue = rawParam.getValue().getValue();
        return rawValue;
    }

    /**
     * 返回传感器的SI(国际单位制)度量值
     * @param mode
     * @return
     */
    public float readySi(short mode) throws IOException {
        ReturnParam<DataF> siParam = new ReturnParam<>(0, DataF.from(0));
        EV3Operation op = EV3InputOpFactory.OpInputDevice.readySi(
                eventBus.getLayer(), portNo, Data8.from(0), Data8.from(mode), Data8.from(1), siParam );
        EV3DirectCommand cmd = new EV3DirectCommand(op);
        eventBus.postCommand(cmd);
        siValue = siParam.getValue().getValue();
        return siValue;
    }

    protected boolean supportPct() {
        return false;
    }

    /**
     * 返回传感器度量值经转换后的百分比值（0-100）
     * @param mode
     * @return
     */
    public int readyPct(short mode) throws IOException {
        if (!supportPct()) {
            pctValue = -1;
            return -1;
        }

        ReturnParam<Data8> pctParam = new ReturnParam<>(0, Data8.from(0));
        EV3Operation op = EV3InputOpFactory.OpInputDevice.readyPct(
                eventBus.getLayer(), portNo, Data8.from(0), Data8.from(mode), Data8.from(1), pctParam );
        EV3DirectCommand cmd = new EV3DirectCommand(op);
        eventBus.postCommand(cmd);
        pctValue = pctParam.getValue().getValue();
        return pctValue;
    }

}
